﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace Q_Link
{
    public partial class frmMain : Form
    {
        BindingSource linkListBindingSource = new BindingSource();
        Dictionary<string, string> shortcutList = new Dictionary<string, string>();
        public List<string> folderList;
        string configFile = Path.GetDirectoryName(Application.ExecutablePath) + "\\" + "Config.xml";

        public frmMain()
        {
            InitializeComponent();
            // position the form on the right bottom corner of the screen
            positionMainForm();
            folderList = new List<string>(FoldersList());
            generateShortcutList();
        }

        void positionMainForm()
        {
            Rectangle r = Screen.PrimaryScreen.WorkingArea;
            this.StartPosition = FormStartPosition.Manual;
            this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width - 10,
                Screen.PrimaryScreen.WorkingArea.Height - this.Height - 10);
        }

        List<string> FoldersList()
        {
            List<string> folders = new List<string>();
            string path;

            if (File.Exists(configFile))
            {
                FileStream stream = new FileStream(configFile, FileMode.Open);
                XmlReader xmlReader = XmlReader.Create(stream);
                try
                {
                    while (xmlReader.Read())
                    {
                        if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "Folder"))
                        {
                            if (xmlReader.HasAttributes)
                            {
                                path = xmlReader.GetAttribute("path");
                                folders.Add(path);
                            }
                        }

                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
                finally
                {
                    stream.Close();
                    xmlReader.Close();
                }
            }
            else
            {
                // shortcutFoldersList is empty.
            }
            return folders;
        }

        public void generateShortcutList()
        {
            shortcutList.Clear();
            dataGridLinkList.Rows.Clear();
            List<string> filePaths = new List<string>();
            foreach (string folder in folderList)
            {
                try
                {
                    // get all .lnk file from shortcutfolder
                    string[] Paths = Directory.GetFiles(@folder, "*.lnk"); // <-- Case-insensitive
                    string[] Paths1 = Directory.GetFiles(@folder, "*.url"); // <-- Case-insensitive
                    filePaths.AddRange(Paths);
                    filePaths.AddRange(Paths1);
                }
                catch
                {
                    MessageBox.Show("Folder: " + folder + " niet gevonden.");
                }
            }

            // store name-fullpath keyvalue pair in shortcutlist dictionary
            // create linklist with filenames and bind to datagridview
            foreach (string name in filePaths)
            {
                string filenameNoExtension = Path.GetFileNameWithoutExtension(name);
                shortcutList.Add(filenameNoExtension, name);
                dataGridLinkList.Rows.Add(filenameNoExtension);
            }
            dataGridLinkList.Sort(Column1, ListSortDirection.Ascending);
        }

        void dataGridLinkList_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                startProgram(e);
            }
        }

        void startProgram(DataGridViewCellEventArgs e)
        {
            string name = dataGridLinkList.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            string path = shortcutList[name];
            try
            {
                Process.Start(path);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Doel niet gevonden!", "Dead Link");
            }
        }

        void TrayMinimizerForm_Resize(object sender, EventArgs e)
        {
            notifyIcon1.BalloonTipTitle = "Q-Link";
            // notifyIcon1.BalloonTipText = "You have successfully minimized your form.";

            if (FormWindowState.Minimized == this.WindowState)
            {
                notifyIcon1.Visible = true;
                // notifyIcon1.ShowBalloonTip(500);
                this.Hide();
            }
            else if (FormWindowState.Normal == this.WindowState)
            {
                notifyIcon1.Visible = false;
            }
        }

        void notifyIcon1_Click(object sender, MouseEventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }

        void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmSettings settings = new frmSettings(this);
            settings.Show();
        }

        void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAbout About = new frmAbout();
            About.Show();
        }

        void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            generateShortcutList();
            positionMainForm();
        }
    }
}
